package vip.specimen.system.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import vip.specimen.system.datasource.entity.SystemPermissionEntity;
import vip.specimen.system.datasource.entity.SystemRoleEntity;
import vip.specimen.system.datasource.entity.SystemUserEntity;
import vip.specimen.system.service.SystemUserService;

/**
 * *@Author: 余浪人
 * *@Date：2021/2/23 17:42
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
public class ShiroRealm extends AuthorizingRealm {
    @Autowired
    SystemUserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        SystemUserEntity userInfo = (SystemUserEntity) principalCollection.getPrimaryPrincipal();
        for (SystemRoleEntity role : userInfo.getSystemRole()) {
            if (role.getRoleStatus() == 1) {  // 角色=》正常
                authorizationInfo.addRole(role.getRoleCode());
                for (SystemPermissionEntity p : role.getPermission()) {
                    if (p.isEnable()) authorizationInfo.addStringPermission(p.getPermission()); // 权限=》启用
                }
            }
        }
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        //实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
        SystemUserEntity userInfo = userService.findByUsername(username);
        if (null == userInfo || !userInfo.isEnable()) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                userInfo,
                userInfo.getPassword(),
                ByteSource.Util.bytes(userInfo.getSalt()),
                getName()
        );
        return authenticationInfo;

    }

    @Override
    public boolean isPermitted(PrincipalCollection principals, String permission) {
        SystemUserEntity user = (SystemUserEntity) principals.getPrimaryPrincipal();
        return user.isAdmin() || super.isPermitted(principals, permission);
    }

    @Override
    public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
        SystemUserEntity user = (SystemUserEntity) principals.getPrimaryPrincipal();
        return user.isAdmin() || super.hasRole(principals, roleIdentifier);
    }
}
